{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# This is for making changes on the fly\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pylab inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy.random import normal\n",
    "from Centerline import Centerline\n",
    "from RiverObs import IteratedRiverObs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nobs = 5\n",
    "\n",
    "# This is the truth\n",
    "\n",
    "npoints = 100\n",
    "x = np.arange(npoints)*2*np.pi/npoints\n",
    "y = np.sin(x)\n",
    "width = 0.2 + 0.1*x\n",
    "\n",
    "xobs = []\n",
    "yobs = []\n",
    "for i in range(npoints):\n",
    "    dx = normal(0.,width[i],nobs)\n",
    "    dy = normal(0.,width[i],nobs)\n",
    "    xobs += (x[i]+dx).tolist()\n",
    "    yobs += (y[i]+dy).tolist()\n",
    "\n",
    "# This is the first guess\n",
    "\n",
    "nc = 25\n",
    "xc = np.arange(nc)*2*np.pi/nc\n",
    "yc = -1*np.ones(nc) + 0.5*xc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(xobs,yobs,'.',alpha=0.1)\n",
    "plot(xc,yc,'o',alpha=0.5,linewidth=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class reach: pass\n",
    "reach.x = xc\n",
    "reach.y = yc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "No smoothing, one iterations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "river_obs = IteratedRiverObs(reach,xobs,yobs)\n",
    "alpha=1\n",
    "max_iter=1\n",
    "river_obs.iterate(max_iter=max_iter,alpha=alpha,tol=1.e-2)\n",
    "xc,yc = river_obs.get_centerline_xy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(xobs,yobs,'.',alpha=0.1)\n",
    "plot(xc,yc,'o',alpha=0.8,linewidth=2)\n",
    "plot(x,y,'--k')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Smoothing, $\\alpha = 1$, 1 iteration "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "river_obs = IteratedRiverObs(reach,xobs,yobs)\n",
    "alpha=1\n",
    "max_iter=1\n",
    "smooth=1\n",
    "weights=True\n",
    "river_obs.iterate(max_iter=max_iter,alpha=alpha,tol=1.e-2,\n",
    "                  smooth=smooth,weights=weights)\n",
    "xc,yc = river_obs.get_centerline_xy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(xobs,yobs,'.',alpha=0.1)\n",
    "plot(xc,yc,'o',alpha=0.8,linewidth=2)\n",
    "plot(x,y,'--k')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "river_obs = IteratedRiverObs(reach,xobs,yobs)\n",
    "alpha=1\n",
    "max_iter=2\n",
    "smooth=1\n",
    "weights=True\n",
    "river_obs.iterate(max_iter=max_iter,alpha=alpha,tol=1.e-2,\n",
    "                  smooth=smooth,weights=weights)\n",
    "xc,yc = river_obs.get_centerline_xy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(xobs,yobs,'.',alpha=0.1)\n",
    "plot(xc,yc,'o',alpha=0.8,linewidth=2)\n",
    "plot(x,y,'--k')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "river_obs = IteratedRiverObs(reach,xobs,yobs)\n",
    "alpha=0.2\n",
    "max_iter=10\n",
    "smooth=1\n",
    "weights=True\n",
    "river_obs.iterate(max_iter=max_iter,alpha=alpha,tol=1.e-2,\n",
    "                  smooth=smooth,weights=weights)\n",
    "xc,yc = river_obs.get_centerline_xy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(xobs,yobs,'.',alpha=0.1)\n",
    "plot(xc,yc,'o',alpha=0.8,linewidth=2)\n",
    "plot(x,y,'--k')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "river_obs = IteratedRiverObs(reach,xobs,yobs)\n",
    "alpha=1\n",
    "max_iter=1\n",
    "smooth=1.e-1\n",
    "weights=True\n",
    "river_obs.iterate(max_iter=max_iter,alpha=alpha,tol=1.e-2,\n",
    "                  smooth=smooth,weights=weights)\n",
    "xc,yc = river_obs.get_centerline_xy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(xobs,yobs,'.',alpha=0.1)\n",
    "plot(xc,yc,'o',alpha=0.8,linewidth=2)\n",
    "plot(x,y,'--k')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "river_obs = IteratedRiverObs(reach,xobs,yobs)\n",
    "alpha=1\n",
    "max_iter=2\n",
    "smooth=1.e-1\n",
    "weights=True\n",
    "river_obs.iterate(max_iter=max_iter,alpha=alpha,tol=1.e-2,\n",
    "                  smooth=smooth,weights=weights)\n",
    "xc,yc = river_obs.get_centerline_xy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(xobs,yobs,'.',alpha=0.1)\n",
    "plot(xc,yc,'o',alpha=0.8,linewidth=2)\n",
    "plot(x,y,'--k')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "river_obs = IteratedRiverObs(reach,xobs,yobs)\n",
    "alpha=1\n",
    "max_iter=1\n",
    "smooth=1.e-2\n",
    "weights=True\n",
    "river_obs.iterate(max_iter=max_iter,alpha=alpha,tol=1.e-2,\n",
    "                  smooth=smooth,weights=weights)\n",
    "xc,yc = river_obs.get_centerline_xy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(xobs,yobs,'.',alpha=0.1)\n",
    "plot(xc,yc,'o',alpha=0.8,linewidth=2)\n",
    "plot(x,y,'--k')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "river_obs = IteratedRiverObs(reach,xobs,yobs)\n",
    "alpha=1\n",
    "max_iter=2\n",
    "smooth=1.e-2\n",
    "weights=True\n",
    "river_obs.iterate(max_iter=max_iter,alpha=alpha,tol=1.e-2,\n",
    "                  smooth=smooth,weights=weights)\n",
    "xc,yc = river_obs.get_centerline_xy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(xobs,yobs,'.',alpha=0.1)\n",
    "plot(xc,yc,'o',alpha=0.8,linewidth=2)\n",
    "plot(x,y,'--k')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "river_obs = IteratedRiverObs(reach,xobs,yobs)\n",
    "alpha=1\n",
    "max_iter=2\n",
    "smooth=1.e-3\n",
    "weights=True\n",
    "river_obs.iterate(max_iter=max_iter,alpha=alpha,tol=1.e-2,\n",
    "                  smooth=smooth,weights=weights)\n",
    "xc,yc = river_obs.get_centerline_xy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(xobs,yobs,'.',alpha=0.1)\n",
    "plot(xc,yc,'o',alpha=0.8,linewidth=2)\n",
    "plot(x,y,'--k')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "river_obs.add_centerline_obs(x,y,width,'width')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xw = river_obs.centerline_obs['width'].x\n",
    "yw = river_obs.centerline_obs['width'].y\n",
    "w = river_obs.centerline_obs['width'].v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(xobs,yobs,'.',alpha=0.1)\n",
    "plot(xc,yc,'kx',alpha=1,linewidth=2)\n",
    "scatter(xw,yw,c=w,s=50,alpha=1,edgecolor='none')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
